package com.yisu.expressway.network;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

/**
 * Created by will on 15/9/14.
 */
public class ApiResultRequest<T> extends ApiModelRequest<ApiResult<T>>{
    private static final String TAG = "ApiResultRequest";
    private TypeToken<T> mResultType;

    public ApiResultRequest(String url, TypeToken<T> resultType, Response.Listener<ApiResult<T>> listener, Response.ErrorListener errorListener) {
        this(Request.Method.GET, url, resultType, null, null, listener, errorListener);
    }

    public ApiResultRequest(String url, TypeToken<T> resultType, ApiModelRequest.ResponseDealListener<ApiResult<T>> responseDealListener, Response.Listener<ApiResult<T>> listener, Response.ErrorListener errorListener) {
        this(Request.Method.GET, url, resultType, null, responseDealListener, listener, errorListener);
    }

    public ApiResultRequest(int method, String url, TypeToken<T> resultType, Response.Listener<ApiResult<T>> listener, Response.ErrorListener errorListener) {
        this(method, url, resultType, null, null, listener, errorListener);
    }

    public ApiResultRequest(int method, String url, TypeToken<T> resultType, ResponseDealListener<ApiResult<T>> responseDealListener, Response.Listener<ApiResult<T>> listener, Response.ErrorListener errorListener) {
        this(method, url, resultType, null, responseDealListener, listener, errorListener);
    }

    public ApiResultRequest(int method, String url, TypeToken<T> resultType, Object requestBody, ResponseDealListener<ApiResult<T>> responseDealListener, Response.Listener<ApiResult<T>> listener, Response.ErrorListener errorListener) {
        super(method, url, null, requestBody, responseDealListener, listener, errorListener);
        mResultType = resultType;
    }

    @Override
    protected Response<ApiResult<T>> parseNetworkResponse(NetworkResponse response){
        try {

            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers, PROTOCOL_CHARSET));

            if(mResultType == null) {
                return Response.error(new ParseError(new IllegalArgumentException("result type is null")));
            }
            final ApiResult<T> result = new ApiResult<>(new JSONObject(jsonString), mResultType);

            if (mResponseDealListener != null){
                mResponseDealListener.onResponseDeal(result);
            }
            saveResponseParams(response);

            return Response.success(result,
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return  Response.error(new ParseError(je));
        }

    }

}
